package DepartmentManagement

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func DepDel(r *gin.Engine) {
	r.POST("/depdel", func(context *gin.Context) {
		var dep model.Department
		var depdoc []model.DepDoc
		if err := context.ShouldBindJSON(&dep); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		if err := global.DB.Model(model.DepDoc{}).Where("dep_id = ?", dep.ID).First(&depdoc).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				if err := global.DB.Unscoped().Model(model.Department{}).Where("id = ?", dep.ID).Delete(&dep).Error; err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					context.JSON(200, gin.H{
						"Msg": "删除成功",
					})
				}
			} else {
				context.JSON(200, gin.H{
					"Msg": err,
				})
			}
		} else {
			context.JSON(200, gin.H{
				"Msg": "因绑定医师无法删除",
			})
		}

	})
}
